﻿using System.Collections.Generic;
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using System.Data.SqlClient;
using System.Globalization;

namespace sistemaAcademicoFinanceiroEscolas
{
    public partial class agendaProfessores : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                sqlc.Open();

                string sqlmes = "select * from mes";

                SqlCommand cmdmes = new SqlCommand(sqlmes, sqlc);
                SqlDataReader myReadermes = cmdmes.ExecuteReader();

                while (myReadermes.Read())
                {
                    ListItem mesSelecione = new ListItem();
                    mesSelecione.Value = myReadermes["codigomes"].ToString();
                    mesSelecione.Text = myReadermes["descricaomes"].ToString();
                    lstMes.Items.Add(mesSelecione);
                }
                cmdmes.Dispose();
                myReadermes.Close();

                string sqlAnoLetivo = "select * from anoLetivo";

                SqlCommand cmdAnoLetivo = new SqlCommand(sqlAnoLetivo, sqlc);
                SqlDataReader myReaderAnoLetivo = cmdAnoLetivo.ExecuteReader();

                while (myReaderAnoLetivo.Read())
                {
                    ListItem anoLetivoSelecione = new ListItem();
                    anoLetivoSelecione.Value = myReaderAnoLetivo["codigoAnoLetivo"].ToString();
                    anoLetivoSelecione.Text = myReaderAnoLetivo["descricaoAnoLetivo"].ToString();
                    if (DateTime.Now.Year.ToString() == myReaderAnoLetivo["descricaoAnoLetivo"].ToString())
                    {
                        anoLetivoSelecione.Selected = true;
                    }
                    lstAnoLetivo.Items.Add(anoLetivoSelecione);
                }
                cmdAnoLetivo.Dispose();
                myReaderAnoLetivo.Close();

                string sqlProfessor = "select * from professor order by nomeprofessor";

                SqlCommand cmdProfessor = new SqlCommand(sqlProfessor, sqlc);
                SqlDataReader myReaderProfessor = cmdProfessor.ExecuteReader();

                while (myReaderProfessor.Read())
                {
                    ListItem ProfessorSelecione = new ListItem();
                    ProfessorSelecione.Value = myReaderProfessor["codigoProfessor"].ToString();
                    ProfessorSelecione.Text = myReaderProfessor["nomeProfessor"].ToString();
                    lstProfessor.Items.Add(ProfessorSelecione);
                }
                cmdProfessor.Dispose();
                myReaderProfessor.Close();

                string sqlDia = "select * from dia";

                SqlCommand cmdDia = new SqlCommand(sqlDia, sqlc);
                SqlDataReader myReaderDia = cmdDia.ExecuteReader();

                while (myReaderDia.Read())
                {
                    ListItem DiaSelecione = new ListItem();
                    DiaSelecione.Value = myReaderDia["codigoDia"].ToString();
                    DiaSelecione.Text = myReaderDia["dia"].ToString();
                    lstDia.Items.Add(DiaSelecione);
                }
                cmdDia.Dispose();
                myReaderDia.Close();

                sqlc.Close();

                lstDia.SelectedValue = DateTime.Now.Day.ToString();
                lstMes.SelectedValue = DateTime.Now.Month.ToString();
            }
        }

        protected void lstMes_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstDia.Items.Clear();

            string mes = lstMes.SelectedValue;
            if (mes.Length == 1)
            {
                mes = "0" + lstMes.SelectedValue;
            }

            int pegarMes = Convert.ToInt16(mes);

            JulianCalendar cal = new JulianCalendar();

            int quantidadeDias = cal.GetDaysInMonth(1, pegarMes);

            int i = 1;
            for (i = 1; i <= quantidadeDias; i++)
            {
                ListItem dia = new ListItem();
                dia.Value = i.ToString();
                dia.Text = i.ToString();

                lstDia.Items.Add(dia);
            }
        }

        protected void btnVerificar_Click(object sender, EventArgs e)
        {

            //limpar os valores dos txts

            txt1.Text = "";
            txt2.Text = "";
            txt3.Text = "";
            txt4.Text = "";
            txt5.Text = "";

            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();

            lstgrupo.Items.Clear();
            lstgrupo2.Items.Clear();
            lstgrupo3.Items.Clear();
            lstgrupo4.Items.Clear();
            lstgrupo5.Items.Clear();

            lstmateria.Items.Clear();
            lstmateria2.Items.Clear();
            lstmateria3.Items.Clear();
            lstmateria4.Items.Clear();
            lstmateria5.Items.Clear();

            //txtValor1.Text = "";
            //txtValor2.Text = "";
            //txtValor3.Text = "";
            //txtValor4.Text = "";
            //txtValor5.Text = "";

            //txt1.Text = "";
            //txt2.Text = "";
            //txt3.Text = "";
            //txt4.Text = "";
            //txt5.Text = "";


            string sqlgrupo = "select * from grupo order by nomegrupo";

            SqlCommand cmdgrupo = new SqlCommand(sqlgrupo, sqlc);
            SqlDataReader myReadergrupo = cmdgrupo.ExecuteReader();

            ListItem primeiro = new ListItem();

            primeiro.Value = "0";
            primeiro.Text = "selecione";

            lstgrupo.Items.Add(primeiro);
            lstgrupo2.Items.Add(primeiro);
            lstgrupo3.Items.Add(primeiro);
            lstgrupo4.Items.Add(primeiro);
            lstgrupo5.Items.Add(primeiro);

            while (myReadergrupo.Read())
            {
                ListItem grupoSelecione = new ListItem();

                ListItem grupoSelecione2 = new ListItem();

                ListItem grupoSelecione3 = new ListItem();

                ListItem grupoSelecione4 = new ListItem();

                ListItem grupoSelecione5 = new ListItem();

                grupoSelecione.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione2.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione2.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione3.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione3.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione4.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione4.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione5.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione5.Text = myReadergrupo["nomegrupo"].ToString();

                lstgrupo.Items.Add(grupoSelecione);
                lstgrupo2.Items.Add(grupoSelecione2);
                lstgrupo3.Items.Add(grupoSelecione3);
                lstgrupo4.Items.Add(grupoSelecione4);
                lstgrupo5.Items.Add(grupoSelecione5);
            }
            cmdgrupo.Dispose();
            myReadergrupo.Close();

            string sqlDiscProf = "select p.codigoProfessor as codigoProfessor, p.nomeProfessor as nomeProfessor, d.codigoDisciplina as codigoDisciplina, ";
            sqlDiscProf = sqlDiscProf + "d.descricaodisciplina as descricaodisciplina, d.codigoSerie, dp.codigoProfessor, dp.codigoDisciplina, s.descricaoSerie as descricaoSerie, s.codigoSerie as codigoSerie";
            sqlDiscProf = sqlDiscProf + " from professor p,";
            sqlDiscProf = sqlDiscProf + " disciplinaProfessor dp, disciplina d, serie s ";
            sqlDiscProf = sqlDiscProf + " where dp.codigoDisciplina = d.codigoDisciplina and ";
            sqlDiscProf = sqlDiscProf + " dp.codigoProfessor = p.codigoProfessor and ";
            sqlDiscProf = sqlDiscProf + " p.codigoProfessor = " + lstProfessor.SelectedValue + " and d.codigoSerie = s.codigoSerie order by d.descricaoDisciplina";

            SqlCommand cmddisciplina = new SqlCommand(sqlDiscProf, sqlc);
            SqlDataReader myReaderDisc = cmddisciplina.ExecuteReader();


            lstmateria.Items.Add(primeiro);
            lstmateria2.Items.Add(primeiro);
            lstmateria3.Items.Add(primeiro);
            lstmateria4.Items.Add(primeiro);
            lstmateria5.Items.Add(primeiro);

            if (myReaderDisc.HasRows)
            {
                while (myReaderDisc.Read())
                {
                    ListItem materia = new ListItem();
                    ListItem materia2 = new ListItem();
                    ListItem materia3 = new ListItem();
                    ListItem materia4 = new ListItem();
                    ListItem materia5 = new ListItem();

                    materia.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia2.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia2.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia3.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia3.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia4.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia4.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia5.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia5.Text = myReaderDisc["descricaodisciplina"].ToString();

                    lstmateria.Items.Add(materia);
                    lstmateria2.Items.Add(materia2);
                    lstmateria3.Items.Add(materia3);
                    lstmateria4.Items.Add(materia4);
                    lstmateria5.Items.Add(materia5);
                }
            }

            myReaderDisc.Close();





            string dia = lstDia.SelectedValue;
            if (dia.Length == 1)
            {
                dia = "0" + lstDia.SelectedValue;
            }

            string mes = lstMes.SelectedValue;
            if (mes.Length == 1)
            {
                mes = "0" + lstMes.SelectedValue;
            }

            string ano = lstAnoLetivo.SelectedItem.Text;


            //o valor da hora deixou de ser necessário aqui ja que agora vem da tabela aula valor e uma vez que
            //os descontos dos alunos sao definidos em valorEspecialAluno
            //o campo valor aula na tabela aula deixou de ser necessário.



            System.Data.SqlClient.SqlConnection sqlj = new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlj.Open();

            string selectAula = "SELECT codigoAula, codigoProfessor, codigoAluno, codigoGrupo, dataAula, horaInicialAula, ";
            selectAula += "horaFinalAula, codigoDisciplina, descricaoAssuntoAula, observacoesAula, ";
            selectAula += "aulaRealizada, valorHora, valorTotalSemDesconto, valorTotalComDesconto, falta, codigoAnoLetivo, linha, ";

            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial FROM aula AS a WHERE (linha = '1') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaInicial, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal FROM aula AS aa WHERE (linha = '1') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaFinal, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial2 FROM aula AS a WHERE (linha = '2') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaInicial2, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal2 FROM aula AS aa WHERE (linha = '2') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaFinal2, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial3 FROM aula AS a WHERE (linha = '3') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaInicial3, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal3 FROM aula AS aa WHERE (linha = '3') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaFinal3, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial4 FROM aula AS a WHERE (linha = '4') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaInicial4, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal4 FROM aula AS aa WHERE (linha = '4') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaFinal4, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial5 FROM aula AS a WHERE (linha = '5') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaInicial5, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal5 FROM aula AS aa WHERE (linha = '5') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')) AS horaFinal5, ";
            selectAula += "(SELECT nomeAluno FROM aluno WHERE (codigoAluno = aula.codigoAluno)) AS aluno ";

            if (dia.Length == 1)
            {
                dia = "0" + dia;
            }
            if (mes.Length == 1)
            {
                mes = "0" + mes;
            }

            selectAula += "FROM aula WHERE (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "')";

            System.Data.SqlClient.SqlCommand cmdselectAula = new System.Data.SqlClient.SqlCommand(selectAula, sqlj);
            System.Data.SqlClient.SqlDataReader drselectAula = cmdselectAula.ExecuteReader();

            string hora1Inicial = "";
            string hora1Final = "";

            string hora2Inicial = "";
            string hora2Final = "";

            string hora3Inicial = "";
            string hora3Final = "";

            string hora4Inicial = "";
            string hora4Final = "";

            string hora5Inicial = "";
            string hora5Final = "";

            if (drselectAula.HasRows)
            {
                while (drselectAula.Read())
                {
                    hora1Inicial = drselectAula["horaInicial"].ToString();
                    hora1Final = drselectAula["horaFinal"].ToString();

                    hora2Inicial = drselectAula["horaInicial2"].ToString();
                    hora2Final = drselectAula["horaFinal2"].ToString();

                    hora3Inicial = drselectAula["horaInicial3"].ToString();
                    hora3Final = drselectAula["horaFinal3"].ToString();

                    hora4Inicial = drselectAula["horaInicial4"].ToString();
                    hora4Final = drselectAula["horaFinal4"].ToString();

                    hora5Inicial = drselectAula["horaInicial5"].ToString();
                    hora5Final = drselectAula["horaFinal5"].ToString();

                    switch (drselectAula["linha"].ToString())
                    {
                        case "1":
                            if (hora1Inicial != "" && hora1Final != "")
                            {
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt1.Text = drselectAula["aluno"].ToString();
                                    hide1.Value = drselectAula["codigoAluno"].ToString();
                                }

                                //txtValor1.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        case "2":
                            if (hora2Inicial != "" && hora2Final != "")
                            {
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo2.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria2.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt2.Text = drselectAula["aluno"].ToString();
                                    hide2.Value = drselectAula["codigoAluno"].ToString();
                                }

                                //txtValor2.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        case "3":
                            if (hora3Inicial != "" && hora3Final != "")
                            {
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo3.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria3.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt3.Text = drselectAula["aluno"].ToString();
                                    hide3.Value = drselectAula["codigoAluno"].ToString();
                                }

                                //txtValor3.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        case "4":
                            if (hora4Inicial != "" && hora4Final != "")
                            {
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo4.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria4.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt4.Text = drselectAula["aluno"].ToString();
                                    hide4.Value = drselectAula["codigoAluno"].ToString();
                                }

                                //txtValor4.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        default:
                            if (hora5Inicial != "" && hora5Final != "")
                            {
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo5.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria5.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt5.Text = drselectAula["aluno"].ToString();
                                    hide5.Value = drselectAula["codigoAluno"].ToString();
                                }

                                //txtValor5.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                    }
                }
            }

            drselectAula.Close();

            sqlj.Close();

            sqlc.Close();

            Panel1.Visible = true;
            BtnSalvar.Visible = true;
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            string hora1Inicial = "";
            string hora1Final = "";
            string hora2Inicial = "";
            string hora2Final = "";
            string hora3Inicial = "";
            string hora3Final = "";
            string hora4Inicial = "";
            string hora4Final = "";
            string hora5Inicial = "";
            string hora5Final = "";
            bool mensagem = true;

            string dia = lstDia.SelectedValue;
            if (dia.Length == 1)
            {
                dia = "0" + lstDia.SelectedValue;
            }
            string mes = lstMes.SelectedValue;
            if (mes.Length == 1)
            {
                mes = "0" + lstMes.SelectedValue;
            }
            string ano = lstAnoLetivo.SelectedValue;
            if (ano.Length == 1)
            {
                ano = "0" + lstAnoLetivo.SelectedValue;
            }

            string selectVerificar = "SELECT codigoAula, codigoProfessor, codigoAluno, codigoGrupo, dataAula, horaInicialAula, ";
            selectVerificar += "horaFinalAula, codigoDisciplina, descricaoAssuntoAula, observacoesAula,  ";
            selectVerificar += "aulaRealizada, valorHora, valorTotalSemDesconto, valorTotalComDesconto, falta, codigoAnoLetivo, linha, meiaHora, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial FROM aula AS a WHERE (linha = '1') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaInicial, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal FROM aula AS aa WHERE (linha = '1')AND (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaFinal, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial2 FROM aula AS a WHERE (linha = '2') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaInicial2, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal2 FROM aula AS aa WHERE (linha = '2') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaFinal2, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial3 FROM aula AS a WHERE (linha = '3') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaInicial3, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal3 FROM aula AS aa WHERE (linha = '3') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaFinal3, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial4 FROM aula AS a WHERE (linha = '4') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaInicial4, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal4 FROM aula AS aa WHERE (linha = '4') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaFinal4, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial5 FROM aula AS a WHERE (linha = '5') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaInicial5, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal5 FROM aula AS aa WHERE (linha = '5') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaFinal5 ";
            selectVerificar += "FROM aula WHERE (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") ";

            linha1(selectVerificar, dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem, ano, lstProfessor.SelectedValue, hora1Inicial, hora1Final, hora2Inicial, hora2Final, hora3Inicial, hora3Final, hora4Inicial, hora4Final, hora5Inicial, hora5Final, mensagem);
            linha2(selectVerificar, dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem, ano, lstProfessor.SelectedValue, hora1Inicial, hora1Final, hora2Inicial, hora2Final, hora3Inicial, hora3Final, hora4Inicial, hora4Final, hora5Inicial, hora5Final, mensagem);
            linha3(selectVerificar, dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem, ano, lstProfessor.SelectedValue, hora1Inicial, hora1Final, hora2Inicial, hora2Final, hora3Inicial, hora3Final, hora4Inicial, hora4Final, hora5Inicial, hora5Final, mensagem);
            linha4(selectVerificar, dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem, ano, lstProfessor.SelectedValue, hora1Inicial, hora1Final, hora2Inicial, hora2Final, hora3Inicial, hora3Final, hora4Inicial, hora4Final, hora5Inicial, hora5Final, mensagem);
            linha5(selectVerificar, dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem, ano, lstProfessor.SelectedValue, hora1Inicial, hora1Final, hora2Inicial, hora2Final, hora3Inicial, hora3Final, hora4Inicial, hora4Final, hora5Inicial, hora5Final, mensagem);
        }

        void linha1(string selectVerificar, string data, string ano, string professor, string hora1Inicial, string hora1Final, string hora2Inicial, string hora2Final, string hora3Inicial, string hora3Final, string hora4Inicial, string hora4Final, string hora5Inicial, string hora5Final, bool mensagem)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide1.Value != "0" || lstgrupo.SelectedValue != "0")
            {
                if (hide1.Value != "0" && lstgrupo.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno
                    hide1.Value = "0";
                    lstgrupo.SelectedValue = "0";
                    lstmateria.SelectedValue = "0";
                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo1();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {

                    string horaInicial = hora1.Value;
                    string horaFinal = hora12.Value;

                    if (horaInicial.Length == 4)
                    {
                        horaInicial = "0" + horaInicial;
                    }
                    if (horaFinal.Length == 4)
                    {
                        horaFinal = "0" + horaFinal;
                    }

                    TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                    if (dif.Hours == 0)
                    {
                        //validação horas iguais

                        //mensagem java script
                        string horasIguais = "<script language=JavaScript>horasIguais1();</script>";
                        Panel2.Controls.Add(new LiteralControl(horasIguais));
                    }
                    else
                    {
                        bool validarInsert = true;

                        SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '1'", sqlc);
                        SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                        if (drVerificar.HasRows)
                        {
                            while (drVerificar.Read())
                            {
                                hora1Inicial = drVerificar["horaInicial"].ToString();
                                hora1Final = drVerificar["horaFinal"].ToString();

                                hora2Inicial = drVerificar["horaInicial2"].ToString();
                                hora2Final = drVerificar["horaFinal2"].ToString();

                                hora3Inicial = drVerificar["horaInicial3"].ToString();
                                hora3Final = drVerificar["horaFinal3"].ToString();

                                hora4Inicial = drVerificar["horaInicial4"].ToString();
                                hora4Final = drVerificar["horaFinal4"].ToString();

                                hora5Inicial = drVerificar["horaInicial5"].ToString();
                                hora5Final = drVerificar["horaFinal5"].ToString();


                                switch (drVerificar["linha"].ToString())
                                {
                                    case "2":
                                        if (hora2Inicial != "" && hora2Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "3":
                                        if (hora3Inicial != "" && hora3Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "4":
                                        if (hora4Inicial != "" && hora4Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;

                                    case "5":
                                        if (hora5Inicial != "" && hora5Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                        drVerificar.Close();

                        if (validarInsert)
                        {
                            string sqlInserirAula = "";

                            if (verificarAulaAluno(hide1.Value, lstgrupo.SelectedValue, data, horaInicial, horaFinal, "1"))
                            {
                                if (hide1.Value != "0")
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    string sqlDeletarAula = "delete from aula where linha = '1' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                    SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                    cmdDelete.ExecuteNonQuery();
                                    if ((lstmateria.SelectedValue != "0"))
                                    {
                                        int i = 0;
                                        while (i < dif.TotalHours)
                                        {
                                            DateTime hF = hI;
                                            hF += new TimeSpan(0, 0, 3600);

                                            string horaI = hI.ToString("HH:mm");
                                            string horaF = hF.ToString("HH:mm");

                                            if (hI.ToString().Length == 4)
                                            {
                                                horaI = "0" + hI.ToString("HH:mm");
                                            }
                                            if (hF.ToString().Length == 4)
                                            {
                                                horaF = "0" + hF.ToString("HH:mm");
                                            }

                                            sqlInserirAula = "INSERT INTO aula ";
                                            sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                            sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                            sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide1.Value + ", " + ano + ", '1', 0, 1)";

                                            if (Convert.ToDateTime(horaFinal) != hF)
                                            {
                                                hI = hF;
                                            }

                                            SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                            cmdInsert.ExecuteNonQuery();

                                            i++;
                                            if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                            {
                                                hF = hI;
                                                hF += new TimeSpan(0, 0, 1800);

                                                horaI = hI.ToString("HH:mm");
                                                horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide1.Value + ", " + ano + ", '1', 1, 1)";

                                                cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                break;
                                            }
                                        }
                                    }

                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar1();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                    mensagem = false;
                                }
                                else
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    string sqlDeletarAula = "delete from aula where linha = '1' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                    SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                    cmdDelete.ExecuteNonQuery();

                                    if ((lstmateria.SelectedValue != "0"))
                                    {
                                        int i = 0;
                                        while (i < dif.TotalHours)
                                        {
                                            DateTime hF = hI;
                                            hF += new TimeSpan(0, 0, 3600);

                                            string horaI = hI.ToString("HH:mm");
                                            string horaF = hF.ToString("HH:mm");

                                            if (hI.ToString().Length == 4)
                                            {
                                                horaI = "0" + hI.ToString("HH:mm");
                                            }
                                            if (hF.ToString().Length == 4)
                                            {
                                                horaF = "0" + hF.ToString("HH:mm");
                                            }
                                            sqlInserirAula = "INSERT INTO aula ";
                                            sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                            sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                            sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo.SelectedValue + ", '1', 0, 2)";

                                            if (Convert.ToDateTime(horaFinal) != hF)
                                            {
                                                hI = hF;
                                            }

                                            SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                            cmdInsert.ExecuteNonQuery();

                                            i++;
                                            if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                            {
                                                hF = hI;
                                                hF += new TimeSpan(0, 0, 1800);

                                                horaI = hI.ToString("HH:mm");
                                                horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo.SelectedValue + ", '1', 1, 2)";

                                                cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                break;
                                            }
                                        }
                                    }

                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar1();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                    mensagem = false;
                                }
                            }
                        }
                        else
                        {
                            //validar já existe aula nessas horas

                            //mensagem java script
                            string existe = "<script language=JavaScript>existe1();</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));
                        }
                    }
                }
            }
            else
            {
                string selectDeletar = "select * from aula where dataAula = '" + data + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and linha = 1 and aulaRealizada <> 1";

                SqlCommand cmd = new SqlCommand(selectDeletar, sqlc);
                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        string delete = "delete from aula where codigoAula = " + dr["codigoAula"].ToString();

                        SqlCommand cmdDeletar = new SqlCommand(delete, sqld);
                        cmdDeletar.ExecuteNonQuery();
                    }
                }

                string finalizar = "<script language=JavaScript>finalizar1();</script>";
                Panel2.Controls.Add(new LiteralControl(finalizar));
            }

            sqlc.Close();
            sqld.Close();
        }

        void linha2(string selectVerificar, string data, string ano, string professor, string hora1Inicial, string hora1Final, string hora2Inicial, string hora2Final, string hora3Inicial, string hora3Final, string hora4Inicial, string hora4Final, string hora5Inicial, string hora5Final, bool mensagem)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide2.Value != "0" || lstgrupo2.SelectedValue != "0")
            {
                if (hide2.Value != "0" && lstgrupo2.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno
                    hide2.Value = "0";
                    lstgrupo2.SelectedValue = "0";
                    lstmateria2.SelectedValue = "0";
                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo2();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = hora2.Value;
                    string horaFinal = hora22.Value;

                    if (horaInicial.Length == 4)
                    {
                        horaInicial = "0" + horaInicial;
                    }
                    if (horaFinal.Length == 4)
                    {
                        horaFinal = "0" + horaFinal;
                    }

                    TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                    if (dif.Hours == 0)
                    {
                        //validação horas iguais

                        //mensagem java script
                        string horasIguais = "<script language=JavaScript>horasIguais2();</script>";
                        Panel2.Controls.Add(new LiteralControl(horasIguais));
                    }
                    else
                    {
                        bool validarInsert = true;

                        SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '2'", sqlc);
                        SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                        if (drVerificar.HasRows)
                        {
                            while (drVerificar.Read())
                            {
                                hora1Inicial = drVerificar["horaInicial"].ToString();
                                hora1Final = drVerificar["horaFinal"].ToString();

                                hora2Inicial = drVerificar["horaInicial2"].ToString();
                                hora2Final = drVerificar["horaFinal2"].ToString();

                                hora3Inicial = drVerificar["horaInicial3"].ToString();
                                hora3Final = drVerificar["horaFinal3"].ToString();

                                hora4Inicial = drVerificar["horaInicial4"].ToString();
                                hora4Final = drVerificar["horaFinal4"].ToString();

                                hora5Inicial = drVerificar["horaInicial5"].ToString();
                                hora5Final = drVerificar["horaFinal5"].ToString();


                                switch (drVerificar["linha"].ToString())
                                {
                                    case "1":
                                        if (hora1Inicial != "" && hora1Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "3":
                                        if (hora3Inicial != "" && hora3Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "4":
                                        if (hora4Inicial != "" && hora4Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;

                                    case "5":
                                        if (hora5Inicial != "" && hora5Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                        drVerificar.Close();

                        if (validarInsert)
                        {
                            string sqlInserirAula = "";
                            if (verificarAulaAluno(hide2.Value, lstgrupo2.SelectedValue, data, horaInicial, horaFinal, "2"))
                            {
                                if (hide2.Value != "0")
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    string sqlDeletarAula = "delete from aula where linha = '2' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                    SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                    cmdDelete.ExecuteNonQuery();
                                    if ((lstmateria2.SelectedValue != "0"))
                                    {
                                        int i = 0;
                                        while (i < dif.TotalHours)
                                        {
                                            DateTime hF = hI;
                                            hF += new TimeSpan(0, 0, 3600);

                                            string horaI = hI.ToString("HH:mm");
                                            string horaF = hF.ToString("HH:mm");

                                            if (hI.ToString().Length == 4)
                                            {
                                                horaI = "0" + hI.ToString("HH:mm");
                                            }
                                            if (hF.ToString().Length == 4)
                                            {
                                                horaF = "0" + hF.ToString("HH:mm");
                                            }

                                            sqlInserirAula = "INSERT INTO aula ";
                                            sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                            sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria2.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                            sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide2.Value + ", " + ano + ", '2', 0, 1)";

                                            if (Convert.ToDateTime(horaFinal) != hF)
                                            {
                                                hI = hF;
                                            }

                                            SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                            cmdInsert.ExecuteNonQuery();

                                            i++;
                                            if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                            {
                                                hF = hI;
                                                hF += new TimeSpan(0, 0, 1800);

                                                horaI = hI.ToString("HH:mm");
                                                horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria2.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide2.Value + ", " + ano + ", '2', 1, 1)";

                                                cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                break;
                                            }
                                        }
                                    }

                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar2();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                    mensagem = false;
                                }
                                else
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    string sqlDeletarAula = "delete from aula where linha = '2' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                    SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                    cmdDelete.ExecuteNonQuery();

                                    if ((lstmateria2.SelectedValue != "0"))
                                    {
                                        int i = 0;
                                        while (i < dif.TotalHours)
                                        {
                                            DateTime hF = hI;
                                            hF += new TimeSpan(0, 0, 3600);

                                            string horaI = hI.ToString("HH:mm");
                                            string horaF = hF.ToString("HH:mm");

                                            if (hI.ToString().Length == 4)
                                            {
                                                horaI = "0" + hI.ToString("HH:mm");
                                            }
                                            if (hF.ToString().Length == 4)
                                            {
                                                horaF = "0" + hF.ToString("HH:mm");
                                            }
                                            sqlInserirAula = "INSERT INTO aula ";
                                            sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                            sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria2.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                            sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo2.SelectedValue + ",'2', 0, 2)";

                                            if (Convert.ToDateTime(horaFinal) != hF)
                                            {
                                                hI = hF;
                                            }

                                            SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                            cmdInsert.ExecuteNonQuery();

                                            i++;
                                            if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                            {
                                                hF = hI;
                                                hF += new TimeSpan(0, 0, 1800);

                                                horaI = hI.ToString("HH:mm");
                                                horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria2.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo2.SelectedValue + ", '2', 1, 2)";

                                                cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                break;
                                            }
                                        }
                                    }

                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar2();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                    mensagem = false;
                                }
                            }
                        }
                        else
                        {
                            //validar já existe aula nessas horas

                            //mensagem java script
                            string existe = "<script language=JavaScript>existe2();</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));
                        }
                    }
                }
            }
            else
            {
                string selectDeletar = "select * from aula where dataAula = '" + data + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and linha = 2 and aulaRealizada <> 1";

                SqlCommand cmd = new SqlCommand(selectDeletar, sqlc);
                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        string delete = "delete from aula where codigoAula = " + dr["codigoAula"].ToString();

                        SqlCommand cmdDeletar = new SqlCommand(delete, sqld);
                        cmdDeletar.ExecuteNonQuery();
                    }
                }

                string finalizar = "<script language=JavaScript>finalizar2();</script>";
                Panel2.Controls.Add(new LiteralControl(finalizar));
            }

            sqlc.Close();
            sqld.Close();
        }

        void linha3(string selectVerificar, string data, string ano, string professor, string hora1Inicial, string hora1Final, string hora2Inicial, string hora2Final, string hora3Inicial, string hora3Final, string hora4Inicial, string hora4Final, string hora5Inicial, string hora5Final, bool mensagem)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide3.Value != "0" || lstgrupo3.SelectedValue != "0")
            {
                if (hide3.Value != "0" && lstgrupo3.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno
                    hide3.Value = "0";
                    lstgrupo3.SelectedValue = "0";
                    lstmateria3.SelectedValue = "0";
                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo3();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = hora3.Value;
                    string horaFinal = hora32.Value;

                    if (horaInicial.Length == 4)
                    {
                        horaInicial = "0" + horaInicial;
                    }
                    if (horaFinal.Length == 4)
                    {
                        horaFinal = "0" + horaFinal;
                    }

                    TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                    if (dif.Hours == 0)
                    {
                        //validação horas iguais

                        //mensagem java script
                        string horasIguais = "<script language=JavaScript>horasIguais3();</script>";
                        Panel2.Controls.Add(new LiteralControl(horasIguais));
                    }
                    else
                    {
                        bool validarInsert = true;

                        SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '3'", sqlc);
                        SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                        if (drVerificar.HasRows)
                        {
                            while (drVerificar.Read())
                            {
                                hora1Inicial = drVerificar["horaInicial"].ToString();
                                hora1Final = drVerificar["horaFinal"].ToString();

                                hora2Inicial = drVerificar["horaInicial2"].ToString();
                                hora2Final = drVerificar["horaFinal2"].ToString();

                                hora3Inicial = drVerificar["horaInicial3"].ToString();
                                hora3Final = drVerificar["horaFinal3"].ToString();

                                hora4Inicial = drVerificar["horaInicial4"].ToString();
                                hora4Final = drVerificar["horaFinal4"].ToString();

                                hora5Inicial = drVerificar["horaInicial5"].ToString();
                                hora5Final = drVerificar["horaFinal5"].ToString();


                                switch (drVerificar["linha"].ToString())
                                {
                                    case "1":
                                        if (hora1Inicial != "" && hora1Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "3":
                                        if (hora3Inicial != "" && hora3Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "4":
                                        if (hora4Inicial != "" && hora4Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;

                                    case "5":
                                        if (hora5Inicial != "" && hora5Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                        drVerificar.Close();

                        if (validarInsert)
                        {
                            string sqlInserirAula = "";
                            if (verificarAulaAluno(hide3.Value, lstgrupo3.SelectedValue, data, horaInicial, horaFinal, "3"))
                            {
                                if (hide3.Value != "0")
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    string sqlDeletarAula = "delete from aula where linha = '3' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                    SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                    cmdDelete.ExecuteNonQuery();
                                    if ((lstmateria3.SelectedValue != "0"))
                                    {
                                        int i = 0;
                                        while (i < dif.TotalHours)
                                        {
                                            DateTime hF = hI;
                                            hF += new TimeSpan(0, 0, 3600);

                                            string horaI = hI.ToString("HH:mm");
                                            string horaF = hF.ToString("HH:mm");

                                            if (hI.ToString().Length == 4)
                                            {
                                                horaI = "0" + hI.ToString("HH:mm");
                                            }
                                            if (hF.ToString().Length == 4)
                                            {
                                                horaF = "0" + hF.ToString("HH:mm");
                                            }

                                            sqlInserirAula = "INSERT INTO aula ";
                                            sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                            sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria3.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                            sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide3.Value + ", " + ano + ", '3', 0, 1)";

                                            if (Convert.ToDateTime(horaFinal) != hF)
                                            {
                                                hI = hF;
                                            }

                                            SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                            cmdInsert.ExecuteNonQuery();

                                            i++;
                                            if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                            {
                                                hF = hI;
                                                hF += new TimeSpan(0, 0, 1800);

                                                horaI = hI.ToString("HH:mm");
                                                horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria3.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide3.Value + ", " + ano + ", '3', 1)";

                                                cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                break;
                                            }
                                        }
                                    }

                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar3();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                    mensagem = false;
                                }
                                else
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    string sqlDeletarAula = "delete from aula where linha = '3' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                    SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                    cmdDelete.ExecuteNonQuery();

                                    if ((lstmateria3.SelectedValue != "0"))
                                    {
                                        int i = 0;
                                        while (i < dif.TotalHours)
                                        {
                                            DateTime hF = hI;
                                            hF += new TimeSpan(0, 0, 3600);

                                            string horaI = hI.ToString("HH:mm");
                                            string horaF = hF.ToString("HH:mm");

                                            if (hI.ToString().Length == 4)
                                            {
                                                horaI = "0" + hI.ToString("HH:mm");
                                            }
                                            if (hF.ToString().Length == 4)
                                            {
                                                horaF = "0" + hF.ToString("HH:mm");
                                            }
                                            sqlInserirAula = "INSERT INTO aula ";
                                            sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                            sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria3.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                            sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo3.SelectedValue + ", '3', 0, 2)";

                                            if (Convert.ToDateTime(horaFinal) != hF)
                                            {
                                                hI = hF;
                                            }

                                            SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                            cmdInsert.ExecuteNonQuery();

                                            i++;
                                            if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                            {
                                                hF = hI;
                                                hF += new TimeSpan(0, 0, 1800);

                                                horaI = hI.ToString("HH:mm");
                                                horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria3.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo3.SelectedValue + ", '3', 1, 2)";

                                                cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                break;
                                            }
                                        }
                                    }

                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar3();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                    mensagem = false;
                                }
                            }
                        }
                        else
                        {
                            //validar já existe aula nessas horas

                            //mensagem java script
                            string existe = "<script language=JavaScript>existe3();</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));
                        }
                    }
                }
            }
            else
            {
                string selectDeletar = "select * from aula where dataAula = '" + data + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and linha = 3 and aulaRealizada <> 1";

                SqlCommand cmd = new SqlCommand(selectDeletar, sqlc);
                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        string delete = "delete from aula where codigoAula = " + dr["codigoAula"].ToString();

                        SqlCommand cmdDeletar = new SqlCommand(delete, sqld);
                        cmdDeletar.ExecuteNonQuery();
                    }
                }

                string finalizar = "<script language=JavaScript>finalizar3();</script>";
                Panel2.Controls.Add(new LiteralControl(finalizar));
            }

            sqlc.Close();
            sqld.Close();
        }

        void linha4(string selectVerificar, string data, string ano, string professor, string hora1Inicial, string hora1Final, string hora2Inicial, string hora2Final, string hora3Inicial, string hora3Final, string hora4Inicial, string hora4Final, string hora5Inicial, string hora5Final, bool mensagem)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide4.Value != "0" || lstgrupo4.SelectedValue != "0")
            {
                if (hide4.Value != "0" && lstgrupo4.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno
                    hide4.Value = "0";
                    lstgrupo4.SelectedValue = "0";
                    lstmateria4.SelectedValue = "0";
                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo4();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = hora4.Value;
                    string horaFinal = hora42.Value;

                    if (horaInicial.Length == 4)
                    {
                        horaInicial = "0" + horaInicial;
                    }
                    if (horaFinal.Length == 4)
                    {
                        horaFinal = "0" + horaFinal;
                    }

                    TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                    if (dif.Hours == 0)
                    {
                        //validação horas iguais

                        //mensagem java script
                        string horasIguais = "<script language=JavaScript>horasIguais4();</script>";
                        Panel2.Controls.Add(new LiteralControl(horasIguais));
                    }
                    else
                    {
                        bool validarInsert = true;

                        SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '4'", sqlc);
                        SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                        if (drVerificar.HasRows)
                        {
                            while (drVerificar.Read())
                            {
                                hora1Inicial = drVerificar["horaInicial"].ToString();
                                hora1Final = drVerificar["horaFinal"].ToString();

                                hora2Inicial = drVerificar["horaInicial2"].ToString();
                                hora2Final = drVerificar["horaFinal2"].ToString();

                                hora3Inicial = drVerificar["horaInicial3"].ToString();
                                hora3Final = drVerificar["horaFinal3"].ToString();

                                hora4Inicial = drVerificar["horaInicial4"].ToString();
                                hora4Final = drVerificar["horaFinal4"].ToString();

                                hora5Inicial = drVerificar["horaInicial5"].ToString();
                                hora5Final = drVerificar["horaFinal5"].ToString();


                                switch (drVerificar["linha"].ToString())
                                {
                                    case "1":
                                        if (hora1Inicial != "" && hora1Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "2":
                                        if (hora2Inicial != "" && hora2Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "4":
                                        if (hora4Inicial != "" && hora4Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;

                                    case "5":
                                        if (hora5Inicial != "" && hora5Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                        drVerificar.Close();

                        if (validarInsert)
                        {
                            string sqlInserirAula = "";
                            if (verificarAulaAluno(hide4.Value, lstgrupo4.SelectedValue, data, horaInicial, horaFinal, "4"))
                            {
                                if (hide4.Value != "0")
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    string sqlDeletarAula = "delete from aula where linha = '4' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                    SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                    cmdDelete.ExecuteNonQuery();
                                    if ((lstmateria4.SelectedValue != "0"))
                                    {
                                        int i = 0;
                                        while (i < dif.TotalHours)
                                        {
                                            DateTime hF = hI;
                                            hF += new TimeSpan(0, 0, 3600);

                                            string horaI = hI.ToString("HH:mm");
                                            string horaF = hF.ToString("HH:mm");

                                            if (hI.ToString().Length == 4)
                                            {
                                                horaI = "0" + hI.ToString("HH:mm");
                                            }
                                            if (hF.ToString().Length == 4)
                                            {
                                                horaF = "0" + hF.ToString("HH:mm");
                                            }

                                            sqlInserirAula = "INSERT INTO aula ";
                                            sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                            sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria4.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                            sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide4.Value + ", " + ano + ",'4', 0, 1)";

                                            if (Convert.ToDateTime(horaFinal) != hF)
                                            {
                                                hI = hF;
                                            }

                                            SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                            cmdInsert.ExecuteNonQuery();
                                            ///////////////////////////////////////////////////////AQUI
                                            i++;
                                            if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                            {
                                                hF = hI;
                                                hF += new TimeSpan(0, 0, 1800);

                                                horaI = hI.ToString("HH:mm");
                                                horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria4.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide4.Value + ", " + ano + ", '4', 1, 1)";

                                                cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                break;
                                            }
                                        }
                                    }

                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar4();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                    mensagem = false;
                                }
                                else
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    string sqlDeletarAula = "delete from aula where linha = '4' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                    SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                    cmdDelete.ExecuteNonQuery();

                                    if ((lstmateria4.SelectedValue != "0"))
                                    {
                                        int i = 0;
                                        while (i < dif.TotalHours)
                                        {
                                            DateTime hF = hI;
                                            hF += new TimeSpan(0, 0, 3600);

                                            string horaI = hI.ToString("HH:mm");
                                            string horaF = hF.ToString("HH:mm");

                                            if (hI.ToString().Length == 4)
                                            {
                                                horaI = "0" + hI.ToString("HH:mm");
                                            }
                                            if (hF.ToString().Length == 4)
                                            {
                                                horaF = "0" + hF.ToString("HH:mm");
                                            }
                                            sqlInserirAula = "INSERT INTO aula ";
                                            sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                            sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria4.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                            sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo4.SelectedValue + ", '4', 0, 2)";

                                            if (Convert.ToDateTime(horaFinal) != hF)
                                            {
                                                hI = hF;
                                            }

                                            SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                            cmdInsert.ExecuteNonQuery();

                                            i++;
                                            if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                            {
                                                hF = hI;
                                                hF += new TimeSpan(0, 0, 1800);

                                                horaI = hI.ToString("HH:mm");
                                                horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria4.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo4.SelectedValue + ",'4', 1, 2)";

                                                cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                break;
                                            }
                                        }
                                    }

                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar4();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                    mensagem = false;
                                }
                            }
                        }
                        else
                        {
                            //validar já existe aula nessas horas

                            //mensagem java script
                            string existe = "<script language=JavaScript>existe4();</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));
                        }
                    }
                }
            }
            else
            {
                string selectDeletar = "select * from aula where dataAula = '" + data + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and linha = 4 and aulaRealizada <> 1";

                SqlCommand cmd = new SqlCommand(selectDeletar, sqlc);
                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        string delete = "delete from aula where codigoAula = " + dr["codigoAula"].ToString();

                        SqlCommand cmdDeletar = new SqlCommand(delete, sqld);
                        cmdDeletar.ExecuteNonQuery();
                    }
                }

                string finalizar = "<script language=JavaScript>finalizar4();</script>";
                Panel2.Controls.Add(new LiteralControl(finalizar));
            }

            sqlc.Close();
            sqld.Close();
        }

        void linha5(string selectVerificar, string data, string ano, string professor, string hora1Inicial, string hora1Final, string hora2Inicial, string hora2Final, string hora3Inicial, string hora3Final, string hora4Inicial, string hora4Final, string hora5Inicial, string hora5Final, bool mensagem)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();
            SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqld.Open();

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide5.Value != "0" || lstgrupo5.SelectedValue != "0")
            {
                if (hide5.Value != "0" && lstgrupo5.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno
                    hide5.Value = "0";
                    lstgrupo5.SelectedValue = "0";
                    lstmateria5.SelectedValue = "0";
                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo5();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = hora5.Value;
                    string horaFinal = hora52.Value;

                    if (horaInicial.Length == 4)
                    {
                        horaInicial = "0" + horaInicial;
                    }
                    if (horaFinal.Length == 4)
                    {
                        horaFinal = "0" + horaFinal;
                    }

                    TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                    if (dif.Hours == 0)
                    {
                        //validação horas iguais

                        //mensagem java script
                        string horasIguais = "<script language=JavaScript>horasIguais5();</script>";
                        Panel2.Controls.Add(new LiteralControl(horasIguais));
                    }
                    else
                    {
                        bool validarInsert = true;

                        SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '5'", sqlc);
                        SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                        if (drVerificar.HasRows)
                        {
                            while (drVerificar.Read())
                            {
                                hora1Inicial = drVerificar["horaInicial"].ToString();
                                hora1Final = drVerificar["horaFinal"].ToString();

                                hora2Inicial = drVerificar["horaInicial2"].ToString();
                                hora2Final = drVerificar["horaFinal2"].ToString();

                                hora3Inicial = drVerificar["horaInicial3"].ToString();
                                hora3Final = drVerificar["horaFinal3"].ToString();

                                hora4Inicial = drVerificar["horaInicial4"].ToString();
                                hora4Final = drVerificar["horaFinal4"].ToString();

                                hora5Inicial = drVerificar["horaInicial5"].ToString();
                                hora5Final = drVerificar["horaFinal5"].ToString();


                                switch (drVerificar["linha"].ToString())
                                {
                                    case "1":
                                        if (hora1Inicial != "" && hora1Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "2":
                                        if (hora2Inicial != "" && hora2Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "3":
                                        if (hora3Inicial != "" && hora3Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;

                                    case "4":
                                        if (hora4Inicial != "" && hora4Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                        drVerificar.Close();

                        if (validarInsert)
                        {
                            string sqlInserirAula = "";
                            if (verificarAulaAluno(hide5.Value, lstgrupo5.SelectedValue, data, horaInicial, horaFinal, "5"))
                            {
                                if (hide5.Value != "0")
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    string sqlDeletarAula = "delete from aula where linha = '5' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                    SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                    cmdDelete.ExecuteNonQuery();
                                    if ((lstmateria5.SelectedValue != "0"))
                                    {
                                        int i = 0;
                                        while (i < dif.TotalHours)
                                        {
                                            DateTime hF = hI;
                                            hF += new TimeSpan(0, 0, 3600);

                                            string horaI = hI.ToString("HH:mm");
                                            string horaF = hF.ToString("HH:mm");

                                            if (hI.ToString().Length == 4)
                                            {
                                                horaI = "0" + hI.ToString("HH:mm");
                                            }
                                            if (hF.ToString().Length == 4)
                                            {
                                                horaF = "0" + hF.ToString("HH:mm");
                                            }

                                            sqlInserirAula = "INSERT INTO aula ";
                                            sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                            sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria5.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                            sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide5.Value + ", " + ano + ", '5', 0, 1)";

                                            if (Convert.ToDateTime(horaFinal) != hF)
                                            {
                                                hI = hF;
                                            }

                                            SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                            cmdInsert.ExecuteNonQuery();
                                            ///////////////////////////////////////////////////////AQUI
                                            i++;
                                            if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                            {
                                                hF = hI;
                                                hF += new TimeSpan(0, 0, 1800);

                                                horaI = hI.ToString("HH:mm");
                                                horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria5.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + hide5.Value + ", " + ano + ", '5', 1, 1)";

                                                cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                break;
                                            }
                                        }
                                    }

                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar5();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                    mensagem = false;
                                }
                                else
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    string sqlDeletarAula = "delete from aula where linha = '5' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + data + "'";

                                    SqlCommand cmdDelete = new SqlCommand(sqlDeletarAula, sqld);
                                    cmdDelete.ExecuteNonQuery();

                                    if ((lstmateria5.SelectedValue != "0"))
                                    {
                                        int i = 0;
                                        while (i < dif.TotalHours)
                                        {
                                            DateTime hF = hI;
                                            hF += new TimeSpan(0, 0, 3600);

                                            string horaI = hI.ToString("HH:mm");
                                            string horaF = hF.ToString("HH:mm");

                                            if (hI.ToString().Length == 4)
                                            {
                                                horaI = "0" + hI.ToString("HH:mm");
                                            }
                                            if (hF.ToString().Length == 4)
                                            {
                                                horaF = "0" + hF.ToString("HH:mm");
                                            }
                                            sqlInserirAula = "INSERT INTO aula ";
                                            sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                            sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria5.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                            sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo5.SelectedValue + ", '5', 0, 2)";

                                            if (Convert.ToDateTime(horaFinal) != hF)
                                            {
                                                hI = hF;
                                            }

                                            SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                            cmdInsert.ExecuteNonQuery();

                                            i++;
                                            if ((Convert.ToDouble(dif.TotalHours) - i) == 0.5)
                                            {
                                                hF = hI;
                                                hF += new TimeSpan(0, 0, 1800);

                                                horaI = hI.ToString("HH:mm");
                                                horaF = hF.ToString("HH:mm");

                                                if (hI.ToString().Length == 4)
                                                {
                                                    horaI = "0" + hI.ToString("HH:mm");
                                                }
                                                if (hF.ToString().Length == 4)
                                                {
                                                    horaF = "0" + hF.ToString("HH:mm");
                                                }

                                                sqlInserirAula = "INSERT INTO aula ";
                                                sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, linha, meiaHora, codigoTipoAula)";
                                                sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria5.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                                sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + data + "', " + ano + ", " + lstgrupo5.SelectedValue + ", '5', 1, 2)";

                                                cmdInsert = new SqlCommand(sqlInserirAula, sqld);
                                                cmdInsert.ExecuteNonQuery();
                                                break;
                                            }
                                        }
                                    }

                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar5();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                    mensagem = false;
                                }
                            }
                        }
                        else
                        {
                            //validar já existe aula nessas horas

                            //mensagem java script
                            string existe = "<script language=JavaScript>existe5();</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));
                        }
                    }
                }
            }
            else
            {
                string selectDeletar = "select * from aula where dataAula = '" + data + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and linha = 5 and aulaRealizada <> 1";

                SqlCommand cmd = new SqlCommand(selectDeletar, sqlc);
                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        string delete = "delete from aula where codigoAula = " + dr["codigoAula"].ToString();

                        SqlCommand cmdDeletar = new SqlCommand(delete, sqld);
                        cmdDeletar.ExecuteNonQuery();
                    }
                }

                string finalizar = "<script language=JavaScript>finalizar5();</script>";
                Panel2.Controls.Add(new LiteralControl(finalizar));
            }

            sqlc.Close();
            sqld.Close();
        }

        bool verificarAulaAluno(string codigoAluno, string codigoGrupo, string dataAula, string horaInicial, string horaFinal, string linha)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();

            string select = "";

            if (codigoAluno != "")
            {
                select = "select aula.*, ";

                select += "(SELECT MIN(horaInicialAula) AS horaInicial FROM aula AS a WHERE  (codigoAluno = aula.codigoAluno) AND (dataAula = aula.dataAula)) AS horaInicialM, ";
                select += "(SELECT MAX(horaFinalAula) AS horaFinal FROM aula AS aa WHERE  (codigoAluno = aula.codigoAluno) AND (dataAula = aula.dataAula)) AS horaFinalM ";

                select += "from aula where dataAula = '" + dataAula + "' and codigoAluno = " + codigoAluno;
            }
            else
            {
                select = "select aula.*, ";

                select += "(SELECT MIN(horaInicialAula) AS horaInicial FROM aula AS a WHERE  (codigoGrupo = aula.codigoGrupo) AND (dataAula = aula.dataAula)) AS horaInicialM, ";
                select += "(SELECT MAX(horaFinalAula) AS horaFinal FROM aula AS aa WHERE  (codigoGrupo = aula.codigoGrupo) AND (dataAula = aula.dataAula)) AS horaFinalM ";

                select += "from aula where dataAula = '" + dataAula + "' and codigoGrupo = " + codigoGrupo;
            }

            SqlCommand cmd = new SqlCommand(select, sqlc);
            SqlDataReader dr = cmd.ExecuteReader();

            string horaIncialLooping, horaFinalLooping, msg;

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    horaIncialLooping = dr["horaInicialM"].ToString();
                    horaFinalLooping = dr["horaFinalM"].ToString();

                    //VERIFICAR SE O INTERVALOS DE HORAS JÁ CADASTRADOS É IGUAL A QUE O USUÁRIO PRETENDE INSERIR
                    // CASO SEJA IGUAL REPRESENTA QUE O ALUNO JÁ POSSUI UM CRONOGRAMA PARA ESSE HORÁRIO
                    if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(horaIncialLooping) &&
                        Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(horaFinalLooping))
                    {
                        msg = "<script language=\"javascript\">alert('O aluno já possui aula agendada neste horário. (Linha " + linha + ")');</script>";
                        Panel2.Controls.Add(new LiteralControl(msg));
                        dr.Close();

                        sqlc.Close();
                        return false;
                    }

                    if (Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(horaIncialLooping) &&
                        Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(horaFinalLooping))
                    {
                        msg = "<script language=\"javascript\">alert('O grupo já possui aula agendada neste horário. (Linha " + linha + ")');</script>";
                        Panel2.Controls.Add(new LiteralControl(msg));
                        dr.Close();

                        sqlc.Close();
                        return false;
                    }
                }
            }

            dr.Close();

            sqlc.Close();
            return true;
        }
    }
}
